package fr.dauphine.bookstore.commun;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.dauphine.bookstore.frontoffice.SessionUtil;
import fr.dauphine.bookstore.modele.User;

/**
 * Avatar de l'utilisateur connecte (sous forme d'image JPEG).
 */
public final class AvatarServlet extends HttpServlet {

	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 6433394209725137522L;

	/**
	 * Traite la methode GET.
	 * 
	 * @param request
	 *            Requete
	 * @param response
	 *            Reponse
	 * @throws ServletException
	 *             Erreur de servlet
	 * @throws IOException
	 *             Erreur d'entree/sortie
	 */
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		User user = SessionUtil.getUser(request.getSession());

		if (user != null && user.getAvatar() != null) {
			response.setContentType("image/jpeg");
			response.getOutputStream().write(user.getAvatar());

		} else {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}
}
